<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Services\Thirdparty\WechatHelper;
use App\Models\LogHelper;
use TestingConfig;
class ThirdPartyController extends  Controller {
    protected $wechatHelper ;

    public function __construct()
    {
        /**
         * 微信帮助类
         */
        parent::__construct();
        if(!$this->wechatHelper){
            $this->wechatHelper = new WechatHelper();
        }

    }

    /**
     * 获取微信分享接口参数
     */
    public function getWxShareOption(Request $request){
        $url = $request->input('url');
        $appid = $request->input('appid');
        if(!$url){
           return apiFailed(null,'url should not be null');
        }
        if($appid){
            $component_base_url=TestingConfig::getValue('wechat.component_share_option_url');
            $url = $component_base_url."?url=".$url."&appid=".$appid;
            LogHelper::log('=======>url   '.$url,'Wx_ticket');
            $options = curlRequest($url);
            $options = json_decode($options,true);
        }else{
            $options = $this->wechatHelper->getWxShareOptions($url);
        }
        LogHelper::log('=======>share_options','Wx_ticket');
        LogHelper::log(print_r($options,true),'Wx_ticket');
        return apiSuccess($options);
    }


    public function getPlatformWxShare(Request $request){
        $url = $request->input('url');
        if(!$url){
            return apiFailed(null,'url should not be null');
        }

        $platform = $request->input('platform');
        if(!$platform){
            return apiFailed(null,'platform is invalid');
        }

        $appidPlatformMapping = [
            //绑定在cheerlife公众号上
            'video' => [
                'appid' => 'wx0404a4b543bf52d0',
                'appsecret' => 'f62f597a8a8a241316a3b47ed25bdbc8',
            ],
            //绑定在名优水果特供上
            'vtest' => [
                'appid' => 'wx151c81885efbbcd7',
                'appsecret' => '5141a2c5e306f5d16f8a536c7fcdd4a7',

            ],
        ];

        $platformArr = array_keys($appidPlatformMapping);
        if(!in_array($platform,$platformArr)){
            return apiFailed(null,'platform is invalid');
        }

        if(!$appidInfo = $appidPlatformMapping[$platform]){
            return apiFailed(null,'platform is not matched');
        }
        $wechatHelper = new WechatHelper($appidInfo['appid'],$appidInfo['appsecret']);
        $options = $wechatHelper->getWxShareOptions($url);
        return apiSuccess($options);

    }

}
